﻿Public Class Frmrename

    Dim newfile As String
    Dim oldfile As String

    Private Sub Frmrename_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Rinomina File"

        TextBox1.Text = My.Computer.FileSystem.GetName(Form1.ListBox1.SelectedItem.ToString)
        TextBox1.Enabled = False
        oldfile = TextBox1.Text.Remove(TextBox1.Text.Count - 4, 4)
        TextBox2.Text = TextBox1.Text.Remove(TextBox1.Text.Count - 4, 4)



    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim percorso = My.Computer.FileSystem.GetParentPath(Form1.ListBox1.SelectedItem.ToString)
        newfile = TextBox2.Text
        Dim estensione As String = TextBox1.Text.Remove(0, TextBox1.Text.Count - 4)
        If newfile.Contains("/") Then
            newfile = newfile.Replace("/", "_")
        End If
        If newfile.Contains("\") Then
            newfile = newfile.Replace("\", "_")
        End If
        If newfile <> oldfile Then
            If My.Computer.FileSystem.FileExists(percorso & "\" & newfile & estensione) = True Then
                MsgBox("Esiste già un file con lo stesso nome")
                Me.Close()
            Else
                My.Computer.FileSystem.RenameFile(Form1.ListBox1.SelectedItem.ToString, newfile & estensione)
                'If MsgBox("Il file esiste già vuoi sovrascriverlo?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                'IO.File.Replace(percorso & "\" & newfile, percorso & "\" & newfile, percorso & "\" & newfile & ".bkp")
                'Else
                'Me.Close()
                'End If
                Form1.Button1_Click(sender, e)
            End If
        End If

        Me.Close()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyCode = Keys.Enter Then
            Button1_Click(sender, e)
        End If
    End Sub
End Class